package com.netty.serial;

import java.io.File;
import java.io.FileInputStream;

import com.utils.GzipUtils;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {

	public static void main(String[] args) throws Exception {
		
		EventLoopGroup group = new NioEventLoopGroup();
		
		Bootstrap b = new Bootstrap();
		b.group(group)
		 .channel(NioSocketChannel.class)
		 .handler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel sc) throws Exception {
				//设置编解码器
				sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
				sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
				sc.pipeline().addLast(new ClientHandler());
			}
		});
		
		ChannelFuture cf = b.connect("127.0.0.1", 8765).sync();
		
		for (int i = 0; i < 5; i++) {
			Req req = new Req();
			req.setId(""+i);
			req.setName("pro"+i);
			req.setRequestMessage("数据信息"+i);
			//上传文件
			String path = System.getProperty("user.dir") + File.separatorChar + "sources" + File.separatorChar + "1.mpp";
			File file = new File(path);
			FileInputStream in = new FileInputStream(file);
			byte []data = new byte[in.available()];
			in.read(data);
			in.close();
			req.setAttachment(GzipUtils.gzip(data));
			cf.channel().writeAndFlush(req);
		}
		
		//等待客户端端口关闭
		cf.channel().closeFuture().sync();
		group.shutdownGracefully();
		
	}
}
